﻿using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace jsCompiler
{
    public partial class Form1 : Form
    {
        List<FileInfo> _files = new List<FileInfo>();
        private string _outFolder;

        public Form1()
        {
            InitializeComponent();
        }

        private void Compile()
        {
            panel1.Controls.Clear();

            string fileoutname = null;
            switch((google.ClosureCompiler.OutputFormat)cboOutputFormat.SelectedValue)
            {
                case google.ClosureCompiler.OutputFormat.json:
                    fileoutname = Path.Combine(folderBrowserDialog1.SelectedPath, txtGoogleCodeFileName.Text + ".json");
                    break;
                case google.ClosureCompiler.OutputFormat.text:
                    fileoutname = Path.Combine(folderBrowserDialog1.SelectedPath, txtGoogleCodeFileName.Text + ".js");
                    break;
                case google.ClosureCompiler.OutputFormat.xml:
                    fileoutname = Path.Combine(folderBrowserDialog1.SelectedPath, txtGoogleCodeFileName.Text + ".xml");
                    break;                    
            }
            

            List<google.ClosureCompiler.OutputInfo> OutputInfos = new List<google.ClosureCompiler.OutputInfo>();
            foreach (google.ClosureCompiler.OutputInfo item in chkOutputInfo.CheckedItems)
                OutputInfos.Add((google.ClosureCompiler.OutputInfo)item);

            StringBuilder builder = new StringBuilder();

                google.ClosureCompiler goog = new google.ClosureCompiler(_files, 
                    txtGoogleCodeFileName.Text, 
                    fileoutname,
                    (google.ClosureCompiler.OutputFormat)cboOutputFormat.SelectedValue,
                    (google.ClosureCompiler.WarningLevel)cboWarningLevel.SelectedValue,
                    (google.ClosureCompiler.CompilationLevel)cboCompilationLevel.SelectedValue,
                    OutputInfos, textBox2.Text,
                    chkPrettyPrint.Checked
                    );

                if ((google.ClosureCompiler.OutputFormat)cboOutputFormat.SelectedValue == google.ClosureCompiler.OutputFormat.xml)
                {
                    string filepath = Path.Combine(_outFolder, txtGoogleCodeFileName.Text + ".js");

                    XmlNode node = goog.Xml.SelectSingleNode("compilationResult/compiledCode");
                    if (node == null)
                        return;

                    using (StreamWriter writer = new StreamWriter(filepath))
                    {
                        writer.Write(node.InnerText);
                        writer.Flush();
                    }


                    LinkLabel link = new LinkLabel();
                    link.Text = "http://closure-compiler.appspot.com/code/" + txtGoogleCodeFileName.Text + ".xml";
                    
                }

            }
        

        private void button2_Click(object sender, EventArgs e)
        {

            DialogResult rslt = openFileDialog1.ShowDialog();

            if(rslt == System.Windows.Forms.DialogResult.OK)
                _files.Add(new FileInfo(openFileDialog1.FileName));

            folderBrowserDialog1.SelectedPath = Path.GetDirectoryName(_files[_files.Count - 1].ToString());
            RebuildForm();

        }

        private void RebuildForm()
        {
            textBox1.ResetText();

            foreach (FileInfo f in _files)
            {
                textBox1.AppendText(f.FullName);
                textBox1.AppendText(Environment.NewLine);
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            _files = new List<FileInfo>();
            RebuildForm();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cboCompilationLevel.DataSource = Enum.GetValues(typeof(google.ClosureCompiler.CompilationLevel));
            cboOutputFormat.DataSource = Enum.GetValues(typeof(google.ClosureCompiler.OutputFormat));
            cboRequestType.DataSource = Enum.GetValues(typeof(google.ClosureCompiler.RequestType));
            cboWarningLevel.DataSource = Enum.GetValues(typeof(google.ClosureCompiler.WarningLevel));
            chkOutputInfo.Items.Add(google.ClosureCompiler.OutputInfo.compiled_code);
            chkOutputInfo.Items.Add(google.ClosureCompiler.OutputInfo.errors);
            chkOutputInfo.Items.Add(google.ClosureCompiler.OutputInfo.statistics);
            chkOutputInfo.Items.Add(google.ClosureCompiler.OutputInfo.warnings);


        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult rslt = folderBrowserDialog1.ShowDialog();
            if (rslt == System.Windows.Forms.DialogResult.OK)
            {
                _outFolder = folderBrowserDialog1.SelectedPath;

                if (String.IsNullOrEmpty(txtGoogleCodeFileName.Text))
                    throw new Exception("Expecting Out Name");

                Compile();
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            DialogResult rsult = openFileDialog1.ShowDialog();
            if (rsult == System.Windows.Forms.DialogResult.OK)
            {
                textBox2.Text = openFileDialog1.FileName;
            }
        }


    }
}
